package net.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.oraycn.esbasic.helpers.StringHelper;
import com.oraycn.esbasic.views.WarpLinearLayout;
import com.oraycn.esframework.core.ClientType;

import net.example.manager.ClientGlobalCache;
import net.example.manager.Manager;
import net.example.manager.GGApplication;
import net.example.model.CommunicateMediaType;
import net.example.model.CommunicateType;
import net.example.model.GGUser;
import net.example.model.event.CallEvent;
import net.example.model.event.RequestEvent;
import net.example.ggtalk.R;
import net.example.utils.ConfigUtils;
import net.example.utils.HeadImgHelper;
import net.example.utils.ToastUtils;
import net.example.utils.android.AndroidUI;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class VideoCall4GroupActivity extends Activity {

    private String  requestorID;
    private ClientType clientType;
    private Button  agree_btn, refuse_btn;
    private TextView tv_nick;
    private ImageView tv_requestor;
    WarpLinearLayout box;
    private String videoGroupID;
    private List<GGUser> members=new ArrayList<>();
    private String[] memberIDs=new String[]{};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_call4_group);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        agree_btn=(Button) findViewById(R.id.btn_answer_call);
        refuse_btn=(Button) findViewById(R.id.btn_refuse_call);
        box=(WarpLinearLayout)findViewById(R.id.other_users_box);
        tv_nick=(TextView)findViewById(R.id.tv_nick);
        tv_requestor=(ImageView) findViewById(R.id.iv_friend_img);
        //对方请求视频
        if (getIntent().hasExtra("event")) {
            RequestEvent event = (RequestEvent) getIntent().getSerializableExtra("event");
            if (event != null) {
                requestorID = event.requestID;
                GGUser requestor=ClientGlobalCache.getInstance().getUser(requestorID);
                if(requestor!=null)
                {
                    tv_nick.setText(requestor.getCommentName());
                    HeadImgHelper.setUserHeadImg(tv_requestor,requestor);
                }
                clientType = event.clientType;
                if(StringHelper.isNullOrEmpty(event.Tag))
                {
                    finish();
                }else {
                    String[] tag=  event.Tag.split(ConfigUtils.ColonSeparator);
                    videoGroupID=tag[0];
                    memberIDs=tag[1].split(ConfigUtils.CommaSeparator);
                    for (String memberID:memberIDs)
                    {
                        //其他参加群聊客户的头像 不包含自己和视频发起者
                        if(memberID.equals(ClientGlobalCache.getInstance().getCurrentUser().getUserID())||memberID.equals(requestorID))
                        {continue;}
                        GGUser member= ClientGlobalCache.getInstance().getUser(memberID);
                        if(member!=null)
                        {
                            members.add(member);
                            addGroupMemberView(member);
                        }
                    }
                }
            }
        }
        GGApplication.getInstance().ringForCalling();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GGApplication.getInstance().stopRingForCalling();
        EventBus.getDefault().unregister(this);
    }

    //显示该群聊中的其他人员头像
    private void addGroupMemberView(GGUser member)
    {
        ImageView imageView=new ImageView(this);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(AndroidUI.Dp2Px(this,40),AndroidUI.Dp2Px(this,40)));
        HeadImgHelper.setUserHeadImg(imageView,member);
        box.addView(imageView);
    }

    public void refuse(View view) {
        Manager.getInstance().sendGroupVideoChat4Reject(this.videoGroupID,memberIDs,CommunicateType.Reject);
        terminate("您已拒绝群视频邀请");
    }

    public void agree(View view) {
        Manager.getInstance().sendMediaCommunicate(requestorID, CommunicateMediaType.GroupVideo, CommunicateType.Agree,this.videoGroupID,-1);
        GGApplication.getInstance().stopRingForCalling();
        finish();
        Intent intent = new Intent(this, VideoChat4GroupActivity.class);
        intent.putExtra("groupID", videoGroupID);
        this.startActivity(intent);
    }

    private void terminate(String message) {
        GGApplication.getInstance().stopRingForCalling();
        ///TO DO  关闭连接器
        String groupID=videoGroupID.split("_")[0];
        Manager.getInstance().sendSystemMessage(groupID,message,true);// 发送系统消息
        finish();
        ToastUtils.showLong(GGApplication.applicationContext, message);
    }

    public void onEventMainThread(CallEvent event) {
        if(event.isEcho)
        {
            terminate("已在其他地方应答了");
            return;
        }
        if(event.mediaType!=CommunicateMediaType.GroupVideo)
        {
            return;
        }
        if(!event.Tag.equals(videoGroupID))
        {return;}

        if (event.communicateType == CommunicateType.Agree) {

        } else if (event.communicateType == CommunicateType.Reject) {
            ToastUtils.showLong(this, ClientGlobalCache.getInstance().getUserName(event.target) + "拒绝了群视频邀请!");
        } else if (event.communicateType == CommunicateType.Terminate) {
            ToastUtils.showLong(this, ClientGlobalCache.getInstance().getUserName(event.target) + "视频已中断!");
        } else if (event.communicateType == CommunicateType.Busy) {
            ToastUtils.showLong(this, ClientGlobalCache.getInstance().getUserName(event.target) + "忙线中!");
        }
    }
}